#include "UserItem.h"

#include "EventNode.h"
#include "EventObject.h"

#include <stdio.h>
#include <stdlib.h>

struct _UserItem
{
	EventObject  parent_instance;
	EventString *password;
	EventString *user_name;
	EventList   *group_list;
	euint64	     phone;
	euint32	     user_id;
};

static void
user_item_finalize(UserItem *self)
{
	event_object_unref(self->password);
	event_object_unref(self->user_name);
	event_object_unref(self->group_list);
}

UserItem *
user_item_new(void)
{
	UserItem *object = MALLOC_NEW(UserItem);

	event_object_init(EVENT_OBJECT(object));
	event_object_set_finalize(object, FINALIZE(user_item_finalize));
	object->password   = NULL;
	object->user_name  = NULL;
	object->group_list = NULL;
	object->phone	   = 0;
	object->user_id	   = 0;

	return object;
}

void
user_item_set_password(UserItem *self, EventString *password)
{
	return_if_fail(self != NULL, user_item_set_password);
	event_object_unref(self->password);
	self->password = EO_REF(password, EventString);
}

EventString *
user_item_get_password(UserItem *self)
{
	return_val_if_fail(self != NULL, user_item_get_password, NULL);
	return self->password;
}

bool
user_item_compare_password(const UserItem *self, const EventString *password)
{
	return_val_if_fail(self != NULL && password != NULL,
			   user_item_compare_password, false);
	return event_string_compare(self->password, password);
}

void
user_item_set_user_name(UserItem *self, EventString *user_name)
{
	return_if_fail(self != NULL, user_item_set_user_name);
	event_object_unref(self->user_name);
	self->user_name = EO_REF(user_name, EventString);
}

EventString *
user_item_dup_user_name(UserItem *self)
{
	return_val_if_fail(self != NULL, user_item_dup_user_name, NULL);
	return EO_REF(self->user_name, EventString);
}

void
user_item_set_group_list(UserItem *self, EventList *list)
{
	return_if_fail(self != NULL, user_item_set_group_list);
	event_object_unref(self->group_list);
	self->group_list = EO_REF(list, EventList);
}

EventList *
user_item_get_group_list(UserItem *self)
{
	return_val_if_fail(self != NULL, user_item_get_group_list, NULL);
	return self->group_list;
}

void
user_item_set_phone(UserItem *self, euint64 phone)
{
	return_if_fail(self != NULL, user_item_set_phone);
	self->phone = phone;
}

euint64
user_item_get_phone(UserItem *self)
{
	return_val_if_fail(self != NULL, user_item_get_phone, 0);
	return self->phone;
}

void
user_item_set_user_id(UserItem *self, euint32 user_id)
{
	return_if_fail(self != NULL, user_item_set_user_id);
	self->user_id = user_id;
}

euint32
user_item_get_user_id(UserItem *self)
{
	return_val_if_fail(self != NULL, user_item_get_user_id, 0);
	return self->user_id;
}

void
user_item_add_group_id(UserItem *u_item, euint32 group_id)
{
	EventList *gid_node;
	euint32	   exist_gid;
	bool	   was_founded = false;
	return_if_fail(u_item != NULL, user_item_add_group_id);

	gid_node = u_item->group_list;

	/* 查找是否有重复的 GroupID */
	while (gid_node) {
		exist_gid = (euint32)EN_GET_D(gid_node, euint64);
		if (exist_gid == group_id) {
			was_founded = true;
			break;
		}
		gid_node = EN_GET_NEXT(gid_node, EventList *);
	}

	if (was_founded)
		return;
	event_list_add_data(u_item->group_list, EPTR((euint64)group_id),
			    EVENT_TYPE_UINT32, NULL);
}

void
user_item_remove_group_id(UserItem *u_item, euint32 group_id)
{
	return_if_fail(u_item != NULL, user_item_remove_group_id);
	event_list_remove_data(&u_item->group_list, EPTR((euint64)group_id));
}

void
__user_item_print(UserItem *self)
{
	EventNode *node;
	if (!self) {
		printf("==============\n**用户条目为空！**\n\n");
		return;
	}
	printf("==============\n用户信息:\n");
	printf("用户名: %s\n", event_string_get_string(self->user_name));
	printf("用户ID: %07u\n", self->user_id);
	printf("手机号: %lu\n", self->phone);
	printf("加入的事件组ID: ");
	node = EVENT_NODE(self->group_list);
	while (node) {
		printf(" %07u ", (euint32)(*(euint64 *)(&node->data)));
		node = node->next;
	}
	printf("\n\n");
}
